package homework.Matrices;

public class gradeManagementSystem {

    public static void main(String[] args) {
        // 初始化成绩数组：5个学生，3门课程
        double[][] grades = {
                {85.5, 90.0, 78.5}, // 学生1
                {92.0, 88.5, 76.0}, // 学生2
                {78.0, 82.5, 90.0}, // 学生3
                {65.5, 70.0, 68.5}, // 学生4
                {88.0, 94.5, 82.0}  // 学生5
        };

        // 学生姓名数组
        String[] studentNames = {"张三", "李四", "王五", "赵六", "钱七"};

        // 课程名称数组
        String[] courseNames = {"数学", "语文", "英语"};

        // 1. 计算并输出每个学生的平均分
        System.out.println("=== 学生平均分 ===");
        double[] studentAverages = calculateStudentAverages(grades);
        for (int i = 0; i < studentAverages.length; i++) {
            System.out.printf("%s的平均分: %.2f\n", studentNames[i], studentAverages[i]);
        }

        // 2. 计算并输出每门课程的平均分
        System.out.println("\n=== 课程平均分 ===");
        double[] courseAverages = calculateCourseAverages(grades);
        for (int i = 0; i < courseAverages.length; i++) {
            System.out.printf("%s的平均分: %.2f\n", courseNames[i], courseAverages[i]);
        }

        // 3. 找出最高分和最低分的学生
        System.out.println("\n=== 最高分和最低分 ===");
        findHighestAndLowest(grades, studentNames);
    }

    // 计算每个学生的平均分
    public static double[] calculateStudentAverages(double[][] grades) {
        double[] averages = new double[grades.length];
        for (int i = 0; i < grades.length; i++) {
            double sum = 0;
            for (int j = 0; j < grades[i].length; j++) {
                sum += grades[i][j];
            }
            averages[i] = sum / grades[i].length;
        }
        return averages;
    }

    // 计算每门课程的平均分
    public static double[] calculateCourseAverages(double[][] grades) {
        if (grades.length == 0) return new double[0];

        double[] averages = new double[grades[0].length];
        for (int j = 0; j < grades[0].length; j++) {
            double sum = 0;
            for (int i = 0; i < grades.length; i++) {
                sum += grades[i][j];
            }
            averages[j] = sum / grades.length;
        }
        return averages;
    }

    // 找出最高分和最低分的学生
    public static void findHighestAndLowest(double[][] grades, String[] studentNames) {
        double maxGrade = Double.MIN_VALUE;
        double minGrade = Double.MAX_VALUE;
        int maxStudentIndex = -1;
        int minStudentIndex = -1;

        for (int i = 0; i < grades.length; i++) {
            double sum = 0;
            for (int j = 0; j < grades[i].length; j++) {
                sum += grades[i][j];
            }
            double average = sum / grades[i].length;

            if (average > maxGrade) {
                maxGrade = average;
                maxStudentIndex = i;
            }
            if (average < minGrade) {
                minGrade = average;
                minStudentIndex = i;
            }
        }

        System.out.printf("最高分学生: %s, 平均分: %.2f\n",
                studentNames[maxStudentIndex], maxGrade);
        System.out.printf("最低分学生: %s, 平均分: %.2f\n",
                studentNames[minStudentIndex], minGrade);
    }
}
